<template>
  <div>
    <el-button type="primary" size="small" @click="addDomain">添加</el-button>
    <el-form :model="addForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
      <div v-for="(item, index) in addForm.moreNotifyObject" :key="item.key">
        <el-row :gutter="20">
          <el-col :span="10">
            <el-form-item  :label="'标题' + index">
              <el-input v-model="item.title"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item :label="'描述' + index">
              <el-input v-model="item.desc"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-button @click="delCul(item)">删除</el-button>
          </el-col>
        </el-row>
      </div>
      <el-form-item>
        <el-button type="primary" size="small"  @click="submitForm('dynamicValidateForm')">提交</el-button>
        <el-button size="small"  @click="resetForm('dynamicValidateForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
name: "index",
  data(){
  return{
    addForm:{
      moreNotifyObject: [
        {
          title: '',
          desc: ''
        }
      ]
    },
  }
  },
  created() {
    this.getCulture();
  },
  methods:{
    async getCulture(){
      const {data: res} = await this.$http.get('dashboard/culture_list')
      if (res.code !== 200) return this.$message.error(res.data.message)
      this.addForm = res.data.content
    },
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const {data: result} = await this.$http.post('dashboard/save_culture', this.addForm)
          if (result.code !== 200) return this.$message.error(result.data.message)
          this.$message.success('处理成功')
          this.getCulture()
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    delCul(item) {
      let index = this.addForm.moreNotifyObject.indexOf(item)
      if (index !== -1) {
        this.addForm.moreNotifyObject.splice(index, 1)
      }
    },
    addDomain() {
      this.addForm.moreNotifyObject.push({
        title: '',
        desc: ''
      })
    }
  }
}
</script>

<style scoped>

</style>
